﻿using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;

namespace PickGold.Api
{
	public static class GdiPlus
	{
		public const string DLL = Common.DLL_GDIPLUS;
		/*/
		/// <summary>
		/// 
		/// </summary>
		/// <param name="graphics"></param>
		/// <param name="text"></param>
		/// <param name="length"></param>
		/// <param name="font"></param>
		/// <param name="positions"></param>
		/// <param name="flags">CmapLookup = 1,Vertical = 2,Advance = 4,LimitSubpixel = 8</param>
		/// <param name="matrix"></param>
		/// <param name="bounds"></param>
		/// <returns></returns>
		[DllImport(DLL, CharSet = CharSet.Unicode)]
		public extern static int GdipMeasureDriverString(IntPtr graphics, string text, int length, IntPtr font, PointF[] positions, int flags, IntPtr matrix, ref RectangleF bounds);

		/// <summary>
		/// 
		/// </summary>
		/// <param name="graphics"></param>
		/// <param name="text"></param>
		/// <param name="length"></param>
		/// <param name="font"></param>
		/// <param name="brush"></param>
		/// <param name="positions"></param>
		/// <param name="flags">CmapLookup = 1,Vertical = 2,Advance = 4,LimitSubpixel = 8</param>
		/// <param name="matrix"></param>
		/// <returns></returns>
		[DllImport(DLL, CharSet = CharSet.Unicode)]
		public extern static int GdipDrawDriverString(IntPtr graphics, string text, int length, IntPtr font, IntPtr brush, PointF[] positions, int flags, IntPtr matrix);

		/// <summary>
		/// 
		/// </summary>
		/// <param name="graphics">Graphics</param>
		/// <param name="text"></param>
		/// <param name="font">Font</param>
		/// <param name="brush">Brush</param>
		/// <param name="positions">控制字符的输出位置。坐标指是字符的左下角，而非左上角（计算机科学家和数学家使用不同的坐标系）。</param>
		public static void DrawDriverString(object graphics, string text, object font, object brush, PointF[] positions)
		{
			GdiPlus.DrawDriverString(graphics, text, font, brush, positions, null);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="graphics">Graphics</param>
		/// <param name="text"></param>
		/// <param name="font">Font</param>
		/// <param name="brush">Brush</param>
		/// <param name="positions"></param>
		/// <param name="matrix">Matrix</param>
		public static void DrawDriverString(object graphics, string text, object font, object brush, PointF[] positions, object matrix)
		{
			if (graphics == null)
				throw new ArgumentNullException("graphics");

			if (text == null)
				throw new ArgumentNullException("text");

			if (font == null)
				throw new ArgumentNullException("font");

			if (brush == null)
				throw new ArgumentNullException("brush");

			if (positions == null)
				throw new ArgumentNullException("positions");

			// Get hGraphics
			var field = graphics.GetType().GetField("nativeGraphics", BindingFlags.Instance | BindingFlags.NonPublic);
			var hGraphics = (IntPtr)field.GetValue(graphics);
			// Get hFont
			field = font.GetType().GetField("nativeFont", BindingFlags.Instance | BindingFlags.NonPublic);
			var hFont = (IntPtr)field.GetValue(font);
			// Get hBrush
			field = brush.GetType().GetField("nativeBrush", BindingFlags.Instance | BindingFlags.NonPublic);
			var hBrush = (IntPtr)field.GetValue(brush);
			// Get hMatrix
			var hMatrix = IntPtr.Zero;
			if (matrix == null)
			{
				field = matrix.GetType().GetField("nativeMatrix", BindingFlags.Instance | BindingFlags.NonPublic);
				hMatrix = (IntPtr)field.GetValue(matrix);
			}
			int result = GdiPlus.GdipDrawDriverString(hGraphics, text, text.Length, hFont, hBrush, positions, (int)DriverStringOptions.CmapLookup, hMatrix);
		}
		/*/
	}
}